﻿$(function () {
    $.templates({
        selectFirstDeep: $('#selectFirstDeep').text()
    }); //加载jsrender模板
    uploadImg();
    bindValidate(ajaxSubmit);
    ChangeDropDown();
    GetFirstDeep();
});

$('#btnSaveReturn').on('click', function () {
    $('#IsRedirect').val(1);
});

$('#btnSave,#btnReturn').on('click', function () {
    $('#IsRedirect').val(0);
});

function ajaxSubmit() {
    var $form = $("#form1");
    var data = $form.serialize();
    var url = "handler.ashx?Operate=AddOrEditFooter";

    msgBox.showMsgWait(txt_Save);
    $.post(url, data, function (ajaxMsg) {
        msgBox.showMsg(ajaxMsg);
        if (ajaxMsg.status === 1) {
            window.location.href = ajaxMsg.url;
        }
        return false;
    }, "json");
    return false;
}

//上传图片
function uploadImg() {
    $('input[choosePic="choosePic"]').on('click', function () {//上传图片
        var fileinput = $(this).siblings("input[type='file']");
        if (fileinput) {
            fileinput.on('change', function () {

                var lbSize = $(this).siblings('span').attr('id');
                var imgFile = $(this).attr('id');
                var hdnSrc = $(this).siblings('input[type="hidden"]').attr('id');
                var imgShow = $(this).siblings('img').attr('id');
                ajaxImageUpload(lbSize, imgFile, hdnSrc, imgShow);

            });
            fileinput.click();
        }
    });
};

//获取第一级菜单
function GetFirstDeep() {
    var data = {
        "SiteId": SiteId,
        "ChannelId": ChannelId,
        "CMId": CMId,
        "ParentId": $("#ddlParent button").attr('fid')
    };
    $.post("Handler.ashx?Operate=GetFiledList", data, function (ajaxMsg) {
        window.msgBox.showMsg(ajaxMsg);
        if (ajaxMsg.status === 1) {
            if (ajaxMsg.data.length > 0) {
                var footerList = ajaxMsg.data;
                if (footerList != null) {
                    var html = $.render.selectFirstDeep(footerList);
                    $('#ddlParent-menu li:eq(0)').siblings().remove();
                    $(html).insertAfter($('#ddlParent-menu li:eq(0)'));
                }
            }
        }
    }, "json");

}

function ChangeDropDown() {
    $(document).on('click', '#ddlParent.dropdown ul li', function () {
        var button = $(this).parents('#ddlParent.dropdown').find('button');
        if (button != undefined) {
            button.html($(this).html() + ' <span class="caret"></span>');
            button.attr('fid', $(this).attr('fid'));
            $('#parentId').val($(this).attr('fid'));
        }
    });
};